/*
 *  Copyright 2007-2008 by Matthias Buch-Kromann <mbk.isv@cbs.dk>.
 *  *
 *  This file is part of the Open-source Dependency Toolkit (OSDT),
 *  see http://code.google.com/p/open-source-dependency-toolkit.
 *  
 *  The OSDT is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as 
 *  published by the Free Software Foundation, either version 3 of 
 *  the License, or (at your option) any later version.
 * 
 *  The OSDT is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public 
 *  License along with the OSDT in the files COPYING and COPYING.LESSER.
 *  If not, see http://www.gnu.org/licenses.
 */

package org.osdtsystem.graphs.io;

import java.util.regex.Pattern;

/**
 *
 * @author Matthias Buch-Kromann <mbk.isv@cbs.dk>
 */
public class OsdtHelper {
    public static String unquote(String s) {
        for (int i = 0; i < osdtUnquotePatterns.length ; ++i)
            s = osdtUnquotePatterns[i].matcher(s).replaceAll(osdtUnquoteReplacements[i]);
        return s;
    }

    // Static patterns for quoting and unquoting OSDT encoded graphs
    public static final Pattern[] osdtQuotePatterns 
            = {Pattern.compile("&"), Pattern.compile("\"")};
    public static final String[] osdtQuoteReplacements 
            = {"&amp;", "&quot;"};
    public static final Pattern[] osdtUnquotePatterns
            = {Pattern.compile("&quot;"), Pattern.compile("&amp;")};
    public static final String[] osdtUnquoteReplacements
            = {"\"", "&"};

    // Regular expressions for OSDT encoding of graphs
    public static final Pattern osdtViewPattern = Pattern.compile(
            "\\s*VIEW([0-9]+)\\s+\"([^\"]+)\"\\s+((\\S+)\\([^)]*\\))\\s*$");
    
    // layer pattern: 2=layer id, 3=layer name, 4=super layer string, 6=feat-val string    
    public static final Pattern osdtLayerPattern = Pattern.compile(
        "^\\s*LAYER([0-9]+)(\\s+\"([^\"]*)\")?((\\s+[0-9]+)*)((\\s+\"[^\"]+\")*)\\s*$");

    // node pattern: 1=node id, 2=feat-val string
    public static final Pattern osdtNodePattern = Pattern.compile(
        "^\\s*NODE([0-9]+)((\\s+\"[^\"]+\"=\"[^\"]*\")*)\\s*$");

    // edge pattern: 1=in-node id, 2=out-node id, 3=feat-val string
    public static final Pattern osdtEdgePattern = Pattern.compile(
        "^\\s*EDGE\\s+([0-9]+)<([0-9]+)((\\s+\"[^\"]+\"=\"[^\"]*\")*)\\s*$");

    // feature-value pattern: 1=feature string, 2=value string
    public static final Pattern osdtFeatureValuePattern = Pattern.compile(
        "\"([^\"]+)\"=\"([^\"]*)\"");

    // feature pattern: 1=feature string
    public static final Pattern osdtFeaturePattern = Pattern.compile(
        "\"([^\"]+)\"");

    // internal feature pattern: 
    public static final Pattern osdtInternalFeaturePattern = Pattern.compile(
            "^#");
    
    // id pattern: 1=id
    public static final Pattern osdtIdPattern = Pattern.compile(
        "\\b([0-9]+)\\b");

    // comment pattern: 
    public static final Pattern osdtCommentPattern = Pattern.compile(
        "^\\s*(#.*|)$");

    // Regular expressions for OSDT encoding of views
    
    public static final Pattern osdtDependencyAlignmentEnginePattern = Pattern.compile(
        "DependencyAlignmentEngine\\(VIEW([0-9]+),\\s*VIEW([0-9]+),\\s*VIEW([0-9]+)," +
            "\\s*LAYER([0-9]+)\\)\\s*$");

    public static final Pattern osdtDependencyTreeEnginePattern = Pattern.compile(
        "DependencyTreeEngine\\(VIEW([0-9]+)\\)\\s*$");

    public static final Pattern osdtNodeCollectionEnginePattern = Pattern.compile(
        "NodeCollectionEngine\\(LAYER([0-9]+)\\)\\s*$");

    public static final Pattern osdtProjectiveTreeEnginePattern = Pattern.compile(
        "ProjectiveTreeEngine\\(VIEW([0-9]+),\\s*NODE([0-9]+),\\s*LAYER([0-9]+)," +
            "\\s*LAYER([0-9]+)\\)\\s*$");

    public static final Pattern osdtTreeEnginePattern = Pattern.compile(
        "TreeEngine\\(VIEW([0-9]+),NODE([0-9]+),LAYER([0-9]+)\\)\\s*$");
    //    "TreeEngine\\(VIEW([0-9]+),\\s*NODE([0-9]+),\\s*LAYER([0-9]+)\\)\\s*$");

    public static final Pattern osdtWordAlignmentEnginePattern = Pattern.compile(
        "WordAlignmentEngine\\(VIEW([0-9]+),\\s*VIEW([0-9]+),\\s*LAYER([0-9]+)\\)\\s*$");
}
